package day02;

import day01.Demo03;

//基于synchronized实现购票功能，票总数100
//模拟两个窗口同时购票 200个购票需求
public class Demo3 {
    int tickets=100;
    public synchronized void buyTicket(){
        String name=Thread.currentThread().getName();
        if(tickets>0){
            tickets--;
            System.out.println(name+"成功售出，剩余"+tickets+"张票");
        }
    }

    public static void main(String[] args) {
        Demo3 d=new Demo3();
        Thread t1=new Thread(()->{
            for (int i = 0; i < 100; i++) {
                d.buyTicket();
            }
        },"窗口一");
        Thread t2=new Thread(()->{
            for (int i = 0; i < 100; i++) {
                d.buyTicket();
            }
        },"窗口二");
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("两个窗口购买完毕，剩余："+d.tickets);
    }
}
